var zTreeSetting = {
    check: {
        enable: true,
        chkboxType: {"Y":"ps", "N":"ps"}
    },
    data: {
        key: {
            name: "text"
        },
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid",
            rootPId: 1
        }
    },
    callback: {
        beforeClick: beforeClick,
        onCheck: onCheck
    }
};

/**
 * 获取zTree选中节点
 * @param treeId
 */
function getValue(treeId){
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    var nodes = treeObj.getCheckedNodes(true);
    return nodes;
}

/**
 * 给zTree设置选中
 * @param treeId
 * @param checkedNodes
 */
function setValue(treeId,checkedNodes){
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    for(var i = 0;i < checkedIds.length;i++){
        var node = treeObj.getNodeByParam("id",checkedIds[i].id);
        treeObj.checkNode(node, true, false, true);
    }
}

/**
 * zTree树点击前触发,点击不在checkbox上时
 * @param treeId
 * @param treeNode
 * @returns {boolean}
 */
function beforeClick(treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj(treeId);
    zTree.checkNode(treeNode, !treeNode.checked, true, true);
    return false;
}

/**
 * zTree选中时触发
 * @param e
 * @param treeId
 * @param treeNode
 */
function onCheck(e, treeId, treeNode) {
    var zTree = $.fn.zTree.getZTreeObj(treeId);
    var snodes = zTree.getCheckedNodes(true);
    var ids = "";
    var names = "";
    for (var i=0, l=snodes.length; i<l; i++) {
        names += snodes[i].text + ",";
        ids += snodes[i].id + ",";
    }
    if (names.length > 0 ) names = names.substring(0, names.length-1);
    if (ids.length > 0 ) ids = ids.substring(0, ids.length-1);
    $("#categoryNames").val(names);
    $("#categoryIds").val(ids);
}

/**
 * 弹出zTree
 * @param inputId
 */
function showMenu(inputId) {
    var catObj = $("#" + inputId);
    var catOffset = $("#" + inputId).offset();
    console.log(1,catOffset.left);
    console.log(2,catOffset.top);
    console.log(3,catObj.outerHeight());
    $("#categoryTreeContent").css({left:catOffset.left + "px", top:catOffset.top + catObj.outerHeight() + "px"}).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
}
/**
 * 隐藏zTree
 * @param inputId
 */
function hideMenu() {
    $("#categoryTreeContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
}
/**
 * 隐藏的触发
 * @param event
 */
function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "citySel" || event.target.id == "categoryTreeContent" || $(event.target).parents("#categoryTreeContent").length>0)) {
        hideMenu();
    }
}
